home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap07 / Checker1 / Checker1.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.7 KB  |  116 lines

  1. /*-------------------------------------------------
  2.    CHECKER1.C -- Mouse Hit-Test Demo Program No. 1
  3.                  (c) Charles Petzold, 1998
  4.   -------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. #define DIVISIONS 5
  9.  
  10. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  11.  
  12. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13.                     PSTR  szCmdLine, int iCmdShow)
  14. {
  15.      static TCHAR szAppName[] = TEXT ("Checker1") ;
  16.      HWND         hwnd ;
  17.      MSG          msg ;
  18.      WNDCLASS     wndclass ;
  19.      
  20.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  21.      wndclass.lpfnWndProc   = WndProc ;
  22.      wndclass.cbClsExtra    = 0 ;
  23.      wndclass.cbWndExtra    = 0 ;
  24.      wndclass.hInstance     = hInstance ;
  25.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  26.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  27.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  28.      wndclass.lpszMenuName  = NULL ;
  29.      wndclass.lpszClassName = szAppName ;
  30.      
  31.      if (!RegisterClass (&wndclass))
  32.      {
  33.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  34.                       szAppName, MB_ICONERROR) ;
  35.           return 0 ;
  36.      }
  37.      
  38.      hwnd = CreateWindow (szAppName, TEXT ("Checker1 Mouse Hit-Test Demo"),
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.      
  44.      ShowWindow (hwnd, iCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.      
  47.      while (GetMessage (&msg, NULL, 0, 0))
  48.      {
  49.           TranslateMessage (&msg) ;
  50.           DispatchMessage (&msg) ;
  51.      }
  52.      return msg.wParam ;
  53. }
  54.  
  55. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  56. {
  57.      static BOOL fState[DIVISIONS][DIVISIONS] ;
  58.      static int  cxBlock, cyBlock ;
  59.      HDC         hdc ;
  60.      int         x, y ;
  61.      PAINTSTRUCT ps ;
  62.      RECT        rect ;
  63.      
  64.      switch (message)
  65.      {
  66.      case WM_SIZE :
  67.           cxBlock = LOWORD (lParam) / DIVISIONS ;
  68.           cyBlock = HIWORD (lParam) / DIVISIONS ;
  69.           return 0 ;
  70.           
  71.      case WM_LBUTTONDOWN :
  72.           x = LOWORD (lParam) / cxBlock ;
  73.           y = HIWORD (lParam) / cyBlock ;
  74.           
  75.           if (x < DIVISIONS && y < DIVISIONS)
  76.           {
  77.                fState [x][y] ^= 1 ;
  78.                
  79.                rect.left   = x * cxBlock ;
  80.                rect.top    = y * cyBlock ;
  81.                rect.right  = (x + 1) * cxBlock ;
  82.                rect.bottom = (y + 1) * cyBlock ;
  83.                
  84.                InvalidateRect (hwnd, &rect, FALSE) ;
  85.           }
  86.           else
  87.                MessageBeep (0) ;
  88.           return 0 ;
  89.           
  90.      case WM_PAINT :
  91.           hdc = BeginPaint (hwnd, &ps) ;
  92.           
  93.           for (x = 0 ; x < DIVISIONS ; x++)
  94.           for (y = 0 ; y < DIVISIONS ; y++)
  95.           {
  96.                Rectangle (hdc, x * cxBlock, y * cyBlock,
  97.                          (x + 1) * cxBlock, (y + 1) * cyBlock) ;
  98.                     
  99.                if (fState [x][y])
  100.                {
  101.                     MoveToEx (hdc,  x    * cxBlock,  y    * cyBlock, NULL) ;
  102.                     LineTo   (hdc, (x+1) * cxBlock, (y+1) * cyBlock) ;
  103.                     MoveToEx (hdc,  x    * cxBlock, (y+1) * cyBlock, NULL) ;
  104.                     LineTo   (hdc, (x+1) * cxBlock,  y    * cyBlock) ;
  105.                }
  106.           }
  107.           EndPaint (hwnd, &ps) ;
  108.           return 0 ;
  109.                
  110.      case WM_DESTROY :
  111.           PostQuitMessage (0) ;
  112.           return 0 ;
  113.      }
  114.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  115. }
  116.